home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / gfx / show / amipeg04.lha / aMiPEG_0.4 / main.c < prev    next >
C/C++ Source or Header  |  1994-04-22  |  7KB  |  350 lines

  1. /*
  2.  *  This is the main part
  3.  */
  4.  
  5. #include "video.h"
  6. #include "proto.h"
  7. #include <sys/types.h>
  8. #include <signal.h>
  9.  
  10. #include "util.h"
  11.  
  12. /* Define buffer length. */
  13.  
  14. #define BUF_LENGTH 65536
  15.  
  16.  
  17. /* Declaration of global variable to hold dither info. */
  18.  
  19. int ditherType;
  20.  
  21. /* Global file pointer to incoming data. */
  22. FILE *input;
  23.  
  24. /* End of File flag. */
  25. static int EOF_flag = 0;
  26.  
  27. /* Loop flag. */
  28. int loopFlag = 0;
  29.  
  30. /* Quiet flag. */
  31. int quietFlag = 0;
  32.  
  33. /* Display image on screen? */
  34. int noDisplayFlag = 0;
  35.  
  36. /* Setjmp/Longjmp env. */
  37. jmp_buf env;
  38.  
  39. /* HAM6 rendering / HAM hires flag */
  40. extern int ham6, lores;
  41.  
  42. /* Method of picture conversion */
  43. void (*DoDitherImage)(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  44.               unsigned char *disp, int h, int w);
  45.  
  46. static char version[]="$VER: aMiPEG 0.4 (compiled on " __DATE__ ", " __TIME__ ")\n";
  47.  
  48. /*
  49.  *--------------------------------------------------------------
  50.  *
  51.  * get_more_data --
  52.  *
  53.  *    Called by correct_underflow in bit parsing utilities to
  54.  *      read in more data.
  55.  *
  56.  * Results:
  57.  *    Input buffer updated, buffer length updated.
  58.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  59.  *
  60.  * Side effects:
  61.  *      None.
  62.  *
  63.  *--------------------------------------------------------------
  64.  */
  65.  
  66. int get_more_data(unsigned int *buf_start, int max_length, int *length_ptr, unsigned int **buf_ptr)
  67. {
  68.   
  69.   int length, num_read, request;
  70.   unsigned char *buffer, *mark;
  71.  
  72.   if (EOF_flag) return 0;
  73.  
  74.   length = *length_ptr;
  75.   buffer = (unsigned char *) *buf_ptr;
  76.  
  77.   if (length > 0) {
  78.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  79.     mark = ((unsigned char *) (buf_start + length));
  80.   }
  81.   else {
  82.     mark = (unsigned char *) buf_start;
  83.     length = 0;
  84.   }
  85.  
  86.   request = (max_length-length)*4;
  87.   
  88.   num_read = fread( mark, 1, request, input);
  89.  
  90.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  91.   {
  92.     int num_read_rounded;
  93.     unsigned char *index;
  94.  
  95.     num_read_rounded = num_read & 0xfffffffc;
  96.  
  97.     /* this can happen only if num_read<request; i.e. end of file reached */
  98.     if( num_read_rounded < num_read )
  99.       { 
  100.      num_read_rounded+=4;
  101.      /* fill in with zeros */
  102.      for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  103.      /* advance to the next 4-byte boundary */
  104.      num_read = num_read_rounded;
  105.       }
  106.   }
  107.   
  108.   if (num_read < 0) {
  109.     return -1;
  110.   }
  111.   else if (num_read == 0) {
  112.     *buf_ptr = buf_start;
  113.     
  114.     /* Make 32 bits after end equal to 0 and 32
  115.        bits after that equal to seq end code
  116.        in order to prevent messy data from infinite
  117.        recursion.
  118.     */
  119.  
  120.     *(buf_start + length) = 0x0;
  121.     *(buf_start + length+1) = SEQ_END_CODE;
  122.  
  123.     EOF_flag = 1;
  124.     return 0;
  125.   }
  126.  
  127.   num_read >>= 2;
  128.  
  129.   *buf_ptr = buf_start;
  130.   *length_ptr = length + num_read;
  131.  
  132.   return 1;
  133. }
  134.  
  135. /*
  136.  *--------------------------------------------------------------
  137.  *
  138.  * int_handler --
  139.  *
  140.  *    Handles Cntl-C interupts..
  141.  *
  142.  * Results:
  143.  *    None.
  144.  *
  145.  * Side effects:
  146.  *    None.
  147.  *
  148.  *--------------------------------------------------------------
  149.  */
  150. void int_handler(int dummy)
  151. {
  152.     if (!quietFlag) fprintf(stderr, "Interrupted!\n");
  153.     if (curVidStream) DestroyVidStream(curVidStream);
  154.     exit(1);
  155. }
  156.  
  157.  
  158. /*
  159.  *--------------------------------------------------------------
  160.  *
  161.  * main --
  162.  *
  163.  *    Parses command line, starts decoding and displaying.
  164.  *
  165.  * Results:
  166.  *    None.
  167.  *
  168.  * Side effects:
  169.  *    None.
  170.  *
  171.  *--------------------------------------------------------------
  172.  */
  173.  
  174. void main(int argc, char **argv)
  175. {
  176.  
  177.   char *name;
  178.   static VidStream *theStream;
  179.   int mark;
  180.   extern int lores;
  181.  
  182.   mark = 1;
  183.   argc--;
  184.  
  185.   name = "";
  186.   input = stdin;
  187.   ditherType = FULL_COLOR_DITHER;
  188.   noDisplayFlag = 0;
  189.  
  190.   while (argc) {
  191.     if (strcmp(argv[mark], "-nop") == 0) {
  192.       TogglePFlag();
  193.       argc--; mark++;
  194.     } else if (strcmp(argv[mark], "-nob") == 0) {
  195.       ToggleBFlag();
  196.       argc--; mark++;
  197.     } else if (strcmp(argv[mark], "-display") == 0) {
  198.       name = argv[++mark];
  199.       argc -= 2; mark++;
  200.     } else if (strcmp(argv[mark], "-dither") == 0) {
  201.       argc--; mark++;
  202.       if (argc < 1) {
  203.     perror("Must specify dither option after -dither flag");
  204.     usage(argv[0]);
  205.       }
  206.       if (strcmp(argv[mark], "gray") == 0) {
  207.     argc--; mark++;
  208.     ditherType = GRAY_DITHER;
  209.       } else if (strcmp(argv[mark], "color") == 0) {
  210.     argc--; mark++;
  211.     ditherType = FULL_COLOR_DITHER;
  212.       } else if (strcmp(argv[mark], "hiresham") == 0) {
  213.     argc--; mark++;
  214.     ditherType = FULL_COLOR_DITHER;
  215.     lores = FALSE;
  216.       } else if (strcmp(argv[mark], "ham6") == 0) {
  217.     argc--; mark++;
  218.     ditherType = FULL_COLOR_DITHER;
  219.     ham6 = TRUE;
  220.       } else if (strcmp(argv[mark], "none") == 0) {
  221.     argc--; mark++;
  222.     ditherType = NO_DITHER;
  223.       } else {
  224.     perror("Illegal dither option.");
  225.     usage(argv[0]);
  226.       }
  227.     } 
  228.     else if (strcmp(argv[mark], "-eachstat") == 0) {
  229.       argc--; mark++;
  230. #ifdef ANALYSIS
  231.       showEachFlag = 1;
  232. #else
  233.       fprintf(stderr, "To use -eachstat, recompile with -DANALYSIS in CFLAGS\n");
  234.       exit(1);
  235. #endif
  236.     }
  237.     else if (strcmp(argv[mark], "-quiet") == 0) {
  238.       argc--; mark++;
  239.       quietFlag = 1;
  240.     }
  241.     else if (strcmp(argv[mark], "-loop") == 0) {
  242.       argc--; mark++;
  243.       loopFlag = 1;
  244.     }
  245.     else if (strcmp(argv[mark], "-no_display") == 0) {
  246.       argc--; mark++;
  247.       noDisplayFlag = 1;
  248.     }
  249.     else if (argv[mark][0] == '-') {
  250.       fprintf(stderr, "Un-recognized flag %s\n",argv[mark]);
  251.       usage(argv[0]);
  252.     }
  253.     else {
  254.       input = fopen(argv[mark], "r");
  255.       if (input == NULL) {
  256.     fprintf(stderr, "Could not open file %s\n", argv[mark]);
  257.     usage(argv[0]);
  258.       }
  259.       argc--; mark++;
  260.     }
  261.   }
  262.  
  263.   signal(SIGINT, int_handler);
  264.  
  265.   init_tables();
  266.   
  267.   switch (ditherType) {
  268.     case GRAY_DITHER:
  269.         InitGrayDisplay(name);
  270.         break;
  271.  
  272.     case FULL_COLOR_DITHER:
  273.         InitColorDither();
  274.         InitColorDisplay(name);
  275.         break;
  276.  
  277.     case NO_DITHER:
  278.         HAM8_draw = (void (*)(void *, int, int)) NoDitherImage;    // method casting ... argh!
  279.         DoDitherImage = NoDitherImage;
  280.         break;
  281.   }
  282.  
  283. /*
  284.  *  The new restart handling has not been checked out very closely with changing
  285.  *  (non)intra scale matrices!
  286.  */
  287.  
  288.   theStream = NewVidStream(BUF_LENGTH);
  289.  
  290.   if (setjmp(env) != 0) {
  291.     mpegInitVidRsrc(); /* fix bug in static first in mpegVidRsrc */
  292.  
  293.     rewind(input);
  294.  
  295.     EOF_flag = 0;
  296.     theStream->bit_offset = 0;
  297.     theStream->buf_length = 0;
  298.     theStream->buffer = NULL;
  299.     totNumFrames = 0;
  300. #ifdef ANALYSIS 
  301.     init_stats();
  302. #endif
  303.   }
  304.  
  305.   realTimeStart = ReadSysClock();
  306.   while (mpegVidRsrc(0, theStream));
  307. }
  308.  
  309.  
  310. /*
  311.  *--------------------------------------------------------------
  312.  *
  313.  * usage --
  314.  *
  315.  *    Print mpeg_play usage
  316.  *
  317.  * Results:
  318.  *    None.
  319.  *
  320.  * Side effects:
  321.  *    exits with a return value -1
  322.  *
  323.  *--------------------------------------------------------------
  324.  */
  325.  
  326. void usage(char *s)  /* program name */
  327. {
  328.     fprintf(stderr, "Usage:\n");
  329.     fprintf(stderr, "mpeg_play\n");
  330.     fprintf(stderr, "          [-nob]\n");
  331.     fprintf(stderr, "          [-nop]\n");
  332.     fprintf(stderr, "          [-dither {gray|color|ham6|hiresham none}]\n");
  333.     fprintf(stderr, "          [-loop]\n");
  334.     fprintf(stderr, "          [-eachstat]\n");
  335.     fprintf(stderr, "          [-no_display]\n");
  336.     fprintf(stderr, "          [-quiet]\n");
  337.     fprintf(stderr, "          file_name\n");
  338.     exit (-1);
  339. }
  340.  
  341.  
  342. /*
  343.  *  Dummy display method
  344.  *
  345.  */
  346. void NoDitherImage(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  347.            unsigned char *disp, int h, int w)
  348. {}
  349.  
  350.